package com.dd.ljzapplication.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dd.ljzapplication.R;
import com.dd.ljzapplication.adapter.BaseRVAdapter;
import com.dd.ljzapplication.adapter.BaseRVAdapter06;
import com.dd.ljzapplication.adapter.TestAdapter;
import com.dd.ljzapplication.adapter.TestNewAdapter;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import base.BaseUtilActivity;

/**
 * Created by LiJZ on 2016/10/21.
 */
public class RecyclerViewActivity extends BaseUtilActivity {

	private RecyclerView mRecyclerView;
	private TestAdapter mAdapter;
	private ArrayList<String> mListData;
	private int count;//记录第几次上拉加载
	private TestNewAdapter mNewAdapter;
	private TextView mTime;
	private LinearLayout mRootView;
	private int time = 0;

	@Override
	protected int setLayoutID() {
		return R.layout.activity_recycler;
	}

	@Override
	protected void init() {

	}

	@Override
	protected void initActionBar() {

	}

	@Override
	protected void initView() {
		mRootView = (LinearLayout) findViewById(R.id.rootview);
		mRecyclerView = (RecyclerView) findViewById(R.id.rv);
		mTime = (TextView) findViewById(R.id.tv_time);
		LinearLayoutManager layoutManager = new LinearLayoutManager(this);
		mRecyclerView.setLayoutManager(layoutManager);
		mListData = new ArrayList<>();
		for (int i = 0; i < 15; i++) {
			mListData.add("data" + (i + 1));
		}
		mAdapter = new TestAdapter(this);

//		mAdapter.setIsAlwaysShow(true);
		mNewAdapter = new TestNewAdapter(this);


//		mRecyclerView.setAdapter(mAdapter);
		mRecyclerView.setAdapter(mNewAdapter);

	}

	@Override
	protected void initEvent() {
		//对根布局的操作
/*		TextView child1 = new TextView(this);
		child1.setText("123456789");
		child1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
		child1.setBackgroundColor(Color.RED);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		child1.setLayoutParams(params);
//		mRootView.addView(child1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0));
		mRootView.addView(child1);*/

//		mRootView.setOnTouchListener(new View.OnTouchListener() {
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//
//				return false;
//			}
//		});


		ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
		scheduledExecutorService.schedule(new Runnable() {
			@Override
			public void run() {
				mTime.setText(++time);
			}
		}, 1, TimeUnit.SECONDS);


		//旧的adapter
		mAdapter.setListData(mListData);
		mAdapter.setOnRVItemClickListener(new BaseRVAdapter.OnRVItemClickListener() {
			@Override
			public void setItemClickListener(View v, int position) {
				Toast.makeText(getApplicationContext(), mListData.get(position), Toast.LENGTH_SHORT).show();
			}
		});

		mAdapter.setIsLoadMore(false);
		mAdapter.setIsAlwaysShow(false);
		mAdapter.setMoreListerner(new BaseRVAdapter.OnLoadMoreListerner() {
			@Override
			public void loadMore() {
				++count;
				for (int i = 0; i < 5; i++) {
					mListData.add("第" + count + "次上拉加载：item" + i);
					mAdapter.notifyDataSetChanged();
				}
				if (count == 3) {
					mAdapter.setIsLoadMore(false);
					mAdapter.setIsAlwaysShow(false);
				}
			}
		});


		////////////////////新Adapter////////////////////////////////
		mNewAdapter.setListData(mListData);

		mNewAdapter.setOnRVItemClickListener(new BaseRVAdapter06.OnRVItemClickListener() {
			@Override
			public void setItemClickListener(View v, int position) {
				Toast.makeText(getApplicationContext(), mListData.get(position), Toast.LENGTH_SHORT).show();
			}
		});


		mNewAdapter.setOnLoadMoreListerner(new BaseRVAdapter06.OnLoadMoreListener() {
			@Override
			public void loadMore() {
				++count;
				for (int i = 0; i < 5; i++) {
					mListData.add("第" + count + "次上拉加载：item" + i);
					mNewAdapter.notifyDataSetChanged();
				}
				if (count == 3) {
					mNewAdapter.setIsLoadMore(false);
//					mNewAdapter.setIsShowFootView(false);
				}
			}
		});

		mNewAdapter.setIsLoadMore(true);
		mNewAdapter.setIsShowFootView(true);
//		final View mFragmentView = View.inflate(this, R.layout.item_footview_custom, null);
//		final View mFragmentView= LayoutInflater.from(this).inflate(R.layout.item_footview_custom, null,false);
//		mNewAdapter.setFootView(null);



	////////////////////////////////////////当滑动到顶部交出触摸事件由根布局处理////////////////////////////////////


	}

	public void next(View view) {

		int lastVisibleItemPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findLastVisibleItemPosition();

		mRecyclerView.scrollToPosition(lastVisibleItemPosition + 1);
	}

	public void back(View view) {
		int firstVisibleItemPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition();
		if (firstVisibleItemPosition > 0)
			mRecyclerView.scrollToPosition(firstVisibleItemPosition - 1);
	}


}
